package com.spring;

import com.spring.bean.Cat;
import com.spring.bean.Person;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;


//https://gitee.com/leifengyang/spring-framework
public class MainTest {

	public static void main(String[] args) {
		test03();
	}


	/**
	 * 父子bean
	 */
	public static void test03(){
		DefaultListableBeanFactory child = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(child);
		xmlBeanDefinitionReader.loadBeanDefinitions("beans2.xml");

		Cat cat = (Cat) child.getBean("cat");
		System.out.println(cat);
	}


	public static void test(){
		/*Resource res = new ClassPathResource("beans2.xml");
		DefaultListableBeanFactory factory= new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions(res);
		factory.getBean("cat");*/

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
		context.getBean("cat");
	}

	public static void test02(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
		Person bean = context.getBean(Person.class);
		System.out.println(bean);
	}

	public static void test01(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		Person bean = context.getBean(Person.class);
		System.out.println(bean);

	}
}
